<template>
  <div>
    <div v-if="data.curUserInfo.status == 3">
      上次注册被拒绝，请重新提交材料
    </div>
    <div v-else-if="data.curUserInfo.status == 1">
      账号已提交注册,再次提交将重新提交材料
    </div>
    <div v-else-if="data.curUserInfo.status == 2">
      账号已通过注册并启用,再次提交注册将要重新审核
    </div>
    <div>&nbsp;</div>
    <el-form label-width="120px">
      <el-form-item label="认证角色">
        <el-select v-model="data.roleValue" placeholder="Select">
          <el-option
            v-for="item in data.indexOptions"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="data.name"></el-input>
      </el-form-item>

      <el-form-item>
        <input type="file" ref="fileRef" id="file" @change="filePathChange" />
        <el-button type="primary" size="mini" @click="uploadFile"
          >确认上传</el-button
        >
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleSubmit">提交注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { reactive, onMounted } from "vue";
import {
  func_getAddress,
  func_getUserInfo,
  func_getUserRole,
  func_signUp,

  saveOnIpfs,
} from "../../api/etherneum/api";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { errorMsg } from "../../config/glossary";
import { ElMessage } from "element-plus";
import { encrypt, getKeyPair } from "../../api/etherneum/jsRSA";
import { aesEncrypt, generateAESKEY } from "../../api/etherneum/jsAES";

export default {
  name: "index",
  components: {},
  setup(props) {
    const store = useStore();
    const router = useRouter();
    const data = reactive({
      roleValue: "",
      indexOptions: [

        {
          value: 2,
          name: "原料商",
        },
        {
          value: 3,
          name: "农户",
        },
        {
          value: 4,
          name: "产品商",
        },
        {
          value: 5,
          name: "零售商",
        },
      ],
      privateData: "",
      name: "",
      phone: "",
      address: "",
      currentRole: "",
      curUserInfo: "",
      passPhrase: "",
    });

    const getAddress = async () => {
      const address = await func_getAddress();
      data.inputAddress = address;
      if (address) {
        store.commit("app/SET_ADDRESS", address);
        let role = await func_getUserRole(address);
        store.commit("app/SET_ROLE", role);
        console.log("getUser address=" + address + " role=" + role);
        data.address = address;
        data.currentRole = role;
        data.curUserInfo = await func_getUserInfo(data.address);
        console.log("cur:" + data.curUserInfo.requestRole);
      }
    };
    const handleSubmit = async () => {
      if (data.currentRole != 0) {
        ElMessage.error("该钱包已注册");
        return;
      }
      let ret = 0;
      if (data.curUserInfo.status==2) {
        ElMessage.error("该账号已注册");
      } else {
        ret = await func_signUp(
          data.roleValue,
          data.name,

        );
        if (ret != 0) {
          ElMessage.error(errorMsg["PermissionDenied"]);
        } else {
          ElMessage.success({
            showClose: true,
            message:
              "成功提交申请",
            duration: 0,
          });
        }
      }
    };
    const filePathChange = () => {
      let res = document.getElementById("file");
      data.fileReady = false;
      const reader = new FileReader();
      reader.readAsArrayBuffer(res.files[0]);
      data.uploadFileName = res.files[0].name;
      reader.onload = (e) => {
        data.fileBlob = e.target.result;
        data.fileReady = true;
        console.log("file:", data.fileBlob);
      };
    };

    const fileDescribe = (name, hash) => {
      return "[文件:" + name + " IPFScid=" + hash + "]\n";
    };
    const uploadFile = async () => {
      console.log("start Upload");
      data.uploadFileHash = await saveOnIpfs(data.fileBlob, (hash) => {
        data.uploadFileHash = hash;
        ElMessage.success("上传成功，cid=" + hash);
        data.privateData += fileDescribe(
          data.uploadFileName,
          data.uploadFileHash
        );
      });
    };
    onMounted(() => {
      getAddress().then(async () => {});
      ethereum.on("accountsChanged", () => {
        getAddress();
      });
    });
    return {
      data,
      filePathChange,
      handleSubmit,
      uploadFile,
    };
  },
};
</script>

<style scoped></style>
